package com.mall.controller;

import com.mall.common.resp.ResponseResult;
import com.mall.common.utils.Security;
import com.mall.controller.vo.CommentVO;
import com.mall.entity.Comment;
import com.mall.entity.Order;
import com.mall.service.CommentService;
import com.mall.service.OrderService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xyl
 * @date 2021-03-06 12:22
 */
@Api(tags = "商品评论接口")
@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CommentService commentService;

    @ApiOperation("添加")
    @PostMapping("add")
    public ResponseResult add(@RequestBody Comment comment) {
        //todo 验证订单状态
        Order order =  orderService.getOrderByGoodsIdAndUserId(comment.getGoodsId(), Security.getCurrentUserId());
        if(ObjectUtils.isEmpty(order)){
            return ResponseResult.error("购买此商品才可以评论哦");
        }else if(2 != order.getOrderStatus()){
            return ResponseResult.error("只有确认收货才可评论哦");
        }
        return ResponseResult.toResponse(commentService.add(comment));
    }

    @ApiOperation("列表")
    @GetMapping("list")
    public List<CommentVO> list(@RequestParam Integer goodsId){
        return commentService.listByGoodsId(goodsId);
    }
}
